<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>高阶函数 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter04.html" />
    
    
    <link rel="prev" href="./chapter02.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="3"
        data-chapter-title="高阶函数"
        data-filepath="chapter03.md"
        data-basepath="."
        data-revision="Sun Feb 09 2020 13:35:09 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        递归函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        匿名函数
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高阶函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        闭包的概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        装饰器的使用
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x9AD8;&#x9636;&#x51FD;&#x6570;">&#x9AD8;&#x9636;&#x51FD;&#x6570;</h1>
<p>&#x5728;Python&#x4E2D;&#xFF0C;&#x51FD;&#x6570;&#x5176;&#x5B9E;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello world&apos;</span>
print(type(test))  <span class="hljs-comment"># &lt;class &apos;function&apos;&gt;</span>
</code></pre>
<p>&#x51FD;&#x6570;&#x5BF9;&#x5E94;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x662F; <code>function</code>,&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x5F53;&#x505A;&#x662F;&#x4E00;&#x79CD;&#x590D;&#x6742;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x65E2;&#x7136;&#x540C;&#x6837;&#x90FD;&#x662F;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x5F53;&#x505A;&#x6570;&#x5B57;&#x6216;&#x8005;&#x5B57;&#x7B26;&#x4E32;&#x6765;&#x5904;&#x7406;&#x3002;</p>
<h2 id="&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x51FD;&#x6570;">&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x51FD;&#x6570;</h2>
<p>&#x5728;Python&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x8BA9;&#x5B83;&#x6765;&#x6307;&#x5411;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x7ED9;&#x51FD;&#x6570;&#x8D77;&#x4E86;&#x4E00;&#x4E2A;&#x522B;&#x540D;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello wrold&apos;</span>

fun = test   <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;fun,&#x8BA9;&#x5B83;&#x6307;&#x5411;&#x4E86; test &#x8FD9;&#x4E2A;&#x51FD;&#x6570;</span>
print(fun())   <span class="hljs-comment"># &#x4F7F;&#x7528;fun()&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x8C03;&#x7528;test&#x8FD9;&#x4E2A;&#x51FD;&#x6570;</span>

print(id(fun))  <span class="hljs-comment"># 1819677672040</span>
print(id(test))  <span class="hljs-comment"># 1819677672040</span>
</code></pre>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x51FD;&#x6570;&#x540E;&#x9762;&#x4E0D;&#x80FD;&#x52A0;&#x62EC;&#x53F7;&#xFF01;&#x52A0;&#x62EC;&#x53F7;&#x8868;&#x793A;&#x7684;&#x662F;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x3002;</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;hello world&apos;</span>

result = test()   <span class="hljs-comment"># &#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x8C03;&#x7528;test&#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x628A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x8D4B;&#x503C;&#x7ED9;result&#x53D8;&#x91CF;</span>
print(result())   <span class="hljs-comment"># &#x8FD9;&#x91CC;&#x4F1A;&#x62A5;&#x9519;  TypeError: &apos;str&apos; object is not callable</span>

fun = test   <span class="hljs-comment"># &#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#x662F;&#x7ED9;test&#x51FD;&#x6570;&#x8D77;&#x4E86;&#x4E00;&#x4E2A;&#x522B;&#x540D;&#xFF0C;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x91CC;&#x7684;test&#x540E;&#x9762;&#x4E0D;&#x80FD;&#x52A0;()</span>
fun()        <span class="hljs-comment"># &#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x522B;&#x540D;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;</span>
</code></pre>
<h2 id="&#x9AD8;&#x9636;&#x51FD;&#x6570;">&#x9AD8;&#x9636;&#x51FD;&#x6570;</h2>
<p>&#x65E2;&#x7136;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x6307;&#x5411;&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x80FD;&#x63A5;&#x6536;&#x53D8;&#x91CF;&#xFF0C;&#x90A3;&#x4E48;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5C31;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x540C;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x5F53;&#x505A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x8FD9;&#x79CD;&#x51FD;&#x6570;&#x7684;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x6211;&#x4EEC;&#x79F0;&#x4E4B;&#x4E3A;&#x9AD8;&#x9636;&#x51FD;&#x6570;&#x3002;</p>
<h3 id="&#x51FD;&#x6570;&#x505A;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;">&#x51FD;&#x6570;&#x505A;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">(age,action)</span>:</span>
    <span class="hljs-keyword">if</span> age &lt; <span class="hljs-number">18</span>:
        print(<span class="hljs-string">&apos;&#x60A8;&#x8FD8;&#x6CA1;&#x6EE1;&#x5341;&#x516B;&#x5C81;&#xFF0C;&#x8BF7;&#x9000;&#x51FA;&apos;</span>)
    action()   <span class="hljs-comment"># &#x628A;&#x53C2;&#x6570;action&#x76F4;&#x63A5;&#x5F53;&#x505A;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x8C03;&#x7528;</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">smoke</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;&#x6211;&#x5DF2;&#x7ECF;&#x5E74;&#x6EE1;&#x5341;&#x516B;&#x5C81;&#x4E86;&#xFF0C;&#x6211;&#x60F3;&#x62BD;&#x70DF;&apos;</span>)

my_action = smoke  <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;my_action&#xFF0C;&#x8BA9;&#x5B83;&#x6307;&#x5411;smoke&#x51FD;&#x6570;</span>
test(<span class="hljs-number">21</span>, my_action)  <span class="hljs-comment"># &#x5C06;my_action&#x4F20;&#x7ED9; test &#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x5B83;&#x7684;&#x53C2;&#x6570;</span>

test(<span class="hljs-number">21</span>,smoke)  <span class="hljs-comment"># &#x8FD8;&#x53EF;&#x4EE5;&#x4E0D;&#x518D;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x76F4;&#x63A5;&#x4F20;&#x5165;&#x51FD;&#x6570;&#x540D;</span>
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;">&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;&#x6211;&#x662F;test&#x51FD;&#x6570;&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;&apos;</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo</span><span class="hljs-params">()</span>:</span>
    print(<span class="hljs-string">&apos;&#x6211;&#x662F;demo&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;&apos;</span>)
    <span class="hljs-keyword">return</span> test  <span class="hljs-comment"># test &#x51FD;&#x6570;&#x4F5C;&#x4E3A;demo&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;</span>

result = demo()  <span class="hljs-comment"># &#x6211;&#x662F;demo&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;  &#x8C03;&#x7528; demo &#x51FD;&#x6570;&#xFF0C;&#x628A;demo&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x8D4B;&#x503C;&#x7ED9; result</span>
print(type(result)) <span class="hljs-comment"># &lt;class &apos;function&apos;&gt;  result &#x7684;&#x7C7B;&#x578B;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;</span>

result() <span class="hljs-comment"># &#x6211;&#x662F;demo&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;    &#x6211;&#x662F;test&#x51FD;&#x6570;&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;   &#x65E2;&#x7136;result&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;() &#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;</span>

demo()()  <span class="hljs-comment"># &#x6211;&#x662F;demo&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;    &#x6211;&#x662F;test&#x51FD;&#x6570;&#x91CC;&#x8F93;&#x5165;&#x7684;&#x5185;&#x5BB9;</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter02.html" class="navigation navigation-prev " aria-label="Previous page: 匿名函数"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter04.html" class="navigation navigation-next " aria-label="Next page: 闭包的概念"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
